package ansi.servlet.reserva;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ansi.modelo.AreaComun;
import ansi.modelo.Reserva;
import ansi.modelo.Residente;
import ansi.negocio.GestionAreaComun;
import ansi.negocio.GestionReserva;
import ansi.negocio.GestionResidente;
import ansi.util.Texto;

/**
 * Servlet implementation class AreaComunRegistroServlet
 */
@WebServlet("/ReservaObtenerServlet")
public class ReservaObtenerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservaObtenerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		editar(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		editar(request, response);
	}
	
	protected void editar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String[] arrIdReserva			= request.getParameterValues("idReserva");
			GestionReserva gestionReserva	= new GestionReserva();
			Reserva reserva		= null;
			if(arrIdReserva != null && arrIdReserva.length > 0) {
				reserva		= gestionReserva.obtener(Texto.getInt(arrIdReserva[0]));
				request.setAttribute("reserva", reserva);
			}
			
			GestionAreaComun gestionAreaComun	= new GestionAreaComun();
			Collection<AreaComun> listAreaComun	= gestionAreaComun.listar();
			request.setAttribute("listAreaComun", listAreaComun);
			
			GestionResidente gestionResidente = new GestionResidente();
			Collection<Residente> listResidente	= gestionResidente.listar();
			request.setAttribute("listResidente", listResidente);
			
			RequestDispatcher rd	= request.getRequestDispatcher("reserva_editar.jsp");
			rd.forward(request, response);
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
}
